package com.bite.forum.controller;


import com.bite.forum.common.AppResult;
import com.bite.forum.common.ResultCode;
import com.bite.forum.exception.ApplicationException;
import com.bite.forum.model.Board;
import com.bite.forum.service.impl.BoardServiceImpl;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/board")
public class BoardController {
    @Value("${bit-forum.index.board-num:9}")
    private Integer indexBoardNum;

    @Autowired
    private BoardServiceImpl boardService;

    @GetMapping("/topList")
    public AppResult<List<Board>> topList(){
        List<Board> boards = boardService.selectByNum(indexBoardNum);

        if(boards == null){
            boards = new ArrayList<>();
        }

        return AppResult.success(boards);
    }

    @GetMapping("/getById")
    public AppResult<Board> getById(@Param("id") @NonNull Long id){
        Board board = boardService.selectByPrimaryKey(id);
        if(board == null || board.getDeleteState() == 1){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(ResultCode.FAILED_BOARD_NOT_EXISTS.getMessage());
        }

        return AppResult.success(board);
    }
}
